#include "page.h"

Page::Page(string url, unsigned long long level)
		: url_(url), level_(level)
{
	//cout << "page just created: " << url_ << endl;
	downloadSelf("http://en.wikipedia.org/wiki/", "tmp_file.html");
	parseAndRecordUrls("tmp_file.html");
}

void Page::downloadSelf(string baseUrl, string filename)
{
	// download the file temporarily
	// :TODO: consider moving much of this to main.cpp or somewhere global,
	//        so we don't keep re-creating so many objects
	CURL* easyhandle = curl_easy_init() ;
	string tmpString = baseUrl  + url_;
	cout << "downloading: level: " << level_
		<< " url: " << tmpString << endl;
	curl_easy_setopt(easyhandle, CURLOPT_URL, tmpString.c_str()) ;
	FILE* file = std::fopen(filename.c_str(), "w") ;
	curl_easy_setopt(easyhandle, CURLOPT_WRITEDATA, file) ;
	curl_easy_perform(easyhandle);
	curl_easy_cleanup(easyhandle);
}

void Page::parseAndRecordUrls(string filename)
{
	// :TODO: REALLY messy code
	// :TODO: Extract the title of the page, and store it in title_

	// search for links to other pages, and store their urls in a temprary stack
	stack<string> tmpUrlStack;
	ifstream inFile;
	inFile.open(filename.c_str());

	if (!inFile) { // make sure we can read the file
		cerr << "Could not read temporary file!";
		return;

	}

	size_t found; // tells us where "/wiki/" is
	string possibleUrl;
	string tmpString;
	bool bad;
	while (inFile >> tmpString) {
		//cout << tmpString << endl;
		found = tmpString.find("href=\"/wiki/");
		// :TODO: This is hard-coded.  Find a better way to do this
		if (found == 0) { // we know that "/wiki" is in the right place
			// go from the end of "/wiki/" to just before the last "
			possibleUrl = tmpString.substr(12, tmpString.length() - 13);


			bad = false; // used as a flag for a bad tag

			if (possibleUrl.find("Wikipedia:") != string::npos) {
				bad = true;
			} else if (possibleUrl.find("File:") != string::npos) {
				bad = true;
			} else if (possibleUrl.find("Special:") != string::npos) {
				bad = true;
			} else if (possibleUrl.find("Main_Page") != string::npos) {
				bad = true;
			} else if (possibleUrl.find("Help:") != string::npos) {
				bad = true;
			} else if (possibleUrl.find("User:") != string::npos) {
				bad = true;
			} else if (possibleUrl.find("User_talk") != string::npos) {
				bad = true;
			} else if (possibleUrl.find("Portal:") != string::npos) {
				bad = true;
			} else if (possibleUrl.find("Category:") != string::npos) {
				bad = true;
			} else if (possibleUrl.find("Category_talk") != string::npos) {
				bad = true;
			} else if (possibleUrl.find("Talk:") != string::npos) {
				bad = true;
			} else if (possibleUrl.find(url_) != string::npos) {
				bad = true;
			}


			if (!bad) {
				//cout << tmpString << endl;
				// create a new page and add it to referencedPages_
				//cout << url_ << " points to " << possibleUrl << endl;
				urlsReferenced_.push_front(possibleUrl);	
			}
		}
	}
}
